<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Withdraw;
use App\Models\Buyer;
use App\Models\WithdrawLog;
use Auth;

class WithdrawController extends Controller
{
    /**
     * 用户提现申请
     */
    public function apply(Request $request)
    {
    	$rs['status']     = false;
    	$data['buyer_id'] = Auth::user()->buyer->id;
    	$buyer 			  = Buyer::find($data['buyer_id']);
    	if($buyer->money<$request->money) {
    		$rs['msg'] = '提现金额不能超出'.$buyer->money.'元';
    		return response()->json($rs);
    	}
        $buyer->freeze_money = $buyer->freeze_money+$request->money;
        $buyer->save();

    	$data['money']    = $request->money;
    	$data['status']   = 1;
    	$flag = Withdraw::create($data);
    	if($flag){
    		$rs['status'] = true;
    		return response()->json($rs);
    	}
    	return response()->json($rs);
    }

    public function getList()
    {
        $rs['status'] = false;
        $where['buyer_id'] = Auth::user()->buyer->id;
        $list = Withdraw::where($where)->paginate(10);
        if ($list) {
            $rs['status'] = true;
            $rs['data']   = $list;
            return response()->json($rs);    
        }
        return $rs;
        
    }

    /**
     * 提现推送消息的列表
     */
    public function withdrawLog()
    {
        $rs['status'] = false;
        $where['user_id'] = Auth::user()->id;
        $list             = WithdrawLog::where($where)
                            ->paginate(10);
        if ($list) {
            $rs['status'] = true;
            $rs['data']   = $list;
            return response()->json($rs);    
        }
        return $rs;                    

    }
}
